package zelix.gui.clickguis.reflection.clickgui;

import java.io.IOException;
import java.util.ArrayList;

import zelix.hack.HackCategory;

import zelix.utils.Wrapper;
import net.minecraft.client.gui.GuiScreen;
import net.minecraft.client.gui.ScaledResolution;

import zelix.gui.clickguis.reflection.clickgui.component.Component;
import zelix.gui.clickguis.reflection.clickgui.component.Frame;

public class ClickGuiManager extends GuiScreen {

	public static ArrayList<Frame> frames;
	public static int color;
	private ArrayList<Effect> effectList = new ArrayList<Effect>();
	
	public ClickGuiManager() {
		this.frames = new ArrayList<Frame>();
		int frameX = 2;
		for(HackCategory category : HackCategory.values()) {
			Frame frame = new Frame(category);
			frame.setX(frameX);
			frames.add(frame);
			frameX += frame.getWidth();
		}

	}
	
	@Override
	public void initGui() {
		
	}
	
	@Override
	public void drawScreen(int mouseX, int mouseY, float partialTicks) {
		//if(Main.instance.settingsManager.getSettingByName(Main.instance.Core.hackManager.getModule("ClickGui"), "Background").getValBoolean())

		//drawDefaultBackground();
		//final ScaledResolution res = new ScaledResolution(mc);

		//GL11.glPushMatrix();
		//GL11.glScalef(0.5f, 0.5f, 0.5f);
		ScaledResolution sr = new ScaledResolution(mc);
		Wrapper.INSTANCE.mc().fontRenderer.drawStringWithShadow("Press \"R\" to restore the position of frames", (sr.getScaledWidth()-Wrapper.INSTANCE.mc().fontRenderer.getStringWidth("Press \"R\" to restore the position of frames"))*2+ Wrapper.INSTANCE.mc().fontRenderer.getStringWidth("Press \"R\" to restore the position of frames")-5, (sr.getScaledHeight()-Wrapper.INSTANCE.mc().fontRenderer.FONT_HEIGHT-1)*2, -1);
		//Wrapper.INSTANCE.mc().renderEngine.bindTexture(clickGuiLogo);
		//GL11.glPopMatrix();
		//drawScaledCustomSizeModalRect((this.width-150)/2, -6, 0, 0, 256, 256, 150, 78, 256, 256);
		for(Frame frame : frames) {
			frame.renderFrame(this.fontRenderer);
			frame.updatePosition(mouseX, mouseY);
			for(Component comp : frame.getComponents()) {
				comp.updateComponent(mouseX, mouseY);
			}
		}
	}
	
	@Override
    protected void mouseClicked(final int mouseX, final int mouseY, final int mouseButton) throws IOException {
		for(Frame frame : frames) {
			if(frame.isWithinHeader(mouseX, mouseY) && mouseButton == 0) {
				frame.setDrag(true);
				frame.dragX = mouseX - frame.getX();
				frame.dragY = mouseY - frame.getY();
			}
			if(frame.isWithinHeader(mouseX, mouseY) && mouseButton == 1) {
				frame.setOpen(!frame.isOpen());
			}
			if(frame.isOpen()) {
				if(!frame.getComponents().isEmpty()) {
					for(Component component : frame.getComponents()) {
						component.mouseClicked(mouseX, mouseY, mouseButton);
					}
				}
			}
		}
	}
	
	@Override
	protected void keyTyped(char typedChar, int keyCode) {
		//if()
		for(Frame frame : frames) {
			if(frame.isOpen() && keyCode != 1) {
				if(!frame.getComponents().isEmpty()) {
					for(Component component : frame.getComponents()) {
						component.keyTyped(typedChar, keyCode);
					}
				}
			}
		}
		if (keyCode == 19) {
			this.frames = new ArrayList<Frame>();
			int frameX = 2;
			for(HackCategory category : HackCategory.values()) {
				Frame frame = new Frame(category);
				ScaledResolution sr = new ScaledResolution(mc);
				if(sr.getScaledWidth() < frame.getWidth()*7)
				{
					
				}
				else{
					frame.setX(frameX);
					frames.add(frame);
					frameX += frame.getWidth();
				}
			}
        }
		if (keyCode == 1) {
            Wrapper.INSTANCE.mc().displayGuiScreen(null);
        }
	}

	
	@Override
    protected void mouseReleased(int mouseX, int mouseY, int state) {
		for(Frame frame : frames) {
			frame.setDrag(false);
		}
		for(Frame frame : frames) {
			if(frame.isOpen()) {
				if(!frame.getComponents().isEmpty()) {
					for(Component component : frame.getComponents()) {
						component.mouseReleased(mouseX, mouseY, state);
					}
				}
			}
		}
	}
	
	@Override
	public boolean doesGuiPauseGame() {
		return true;
	}
}
